package ibmetp.mis.dao;

import ibmetp.mis.vo.Page;
import ibmetp.mis.vo.CodeType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 
 * @author IBMETP067
 *
 */
public class CodeTypeDao extends BaseDao{
	private Connection conn;

	/**
	 * 通过代码类型ID获取代码类型信息
	 * @code id
	 * @return
	 */
	public CodeType getCodeTypeById(Long id){
		String sql="select r.* from BAS_T_CODE_TYPE r where r.CODE_TYPE_ID=?";
		System.out.println(sql);
		conn=DBUtil.getConnection();
		PreparedStatement ps;
		CodeType codetype=null;
		CodeTypeDao ct = new CodeTypeDao();
		try{
			ps=conn.prepareStatement(sql);
			ps.setLong(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs!=null && rs.next())
			{
				codetype=new CodeType();
				codetype.setCodeTypeId(rs.getLong("CODE_TYPE_ID"));
				codetype.setCodeTypeName(rs.getString("CODE_TYPE_NAME"));
				codetype.setDescript(rs.getString("DESCRIPT"));
			}
			ps.close();
			conn.close();			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return codetype;
	}
	
	/**
	 * 通过调用对象codetypeVO添加数据
	 * @code codetypeVO
	 * @return
	 */
	public String addCodeType(CodeType codetypeVO) 
	{
		String addSQL="insert into BAS_T_CODE_TYPE (CODE_TYPE_ID,CODE_TYPE_NAME,DESCRIPT) values (CODE_TYPE_SEQ.NEXTVAL,?,?) ";
		//String sql="insert into BAS_T_RESOURCE (RESOURCE_ID,PARENT_ID,RESOURCE_NAME,URL, DESCRIPT)values (RES_SEQ.NEXTVAL,?,?,?,?) ";
		conn=DBUtil.getConnection();
		try{
			PreparedStatement ps= conn.prepareStatement(addSQL);
			//ps.setLong(1, codetypeVO.getCodeTypeId());
			ps.setString(1, codetypeVO.getCodeTypeName());
			ps.setString(2, codetypeVO.getDescript());
			ResultSet rs = ps.executeQuery();
			ps.close();
			conn.close();
//		return ps.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 通过userIds删除数据
	 * @param CodeTypeId
	 * @return
	 * @throws SQLException
	 */
	
	public void deleteCodeTypeById(String CodeTypeIds)
	{
		String delSQL="delete BAS_T_CODE_TYPE where CODE_TYPE_ID in ("+CodeTypeIds+")";
//		String delSQL="delete CODETYPE where USER_ID in ("+array2String(CodeTypeIds)+")";
		conn=DBUtil.getConnection();
		Statement ps;
		try {
			ps = conn.createStatement();
//			String ids=CodeTypeIds;
//			String ids=array2String(CodeTypeIds);
			int i= ps.executeUpdate(delSQL);
			ps.close();
			conn.close();
		}catch(SQLException e){
			e.printStackTrace();
		}			
	}
	
	/**
	 * 通过调用对象codetypeVO更新数据
	 * @param codetypeVO
	 * @throws SQLException
	 */
	
	public void updateCodeType(CodeType codetypeVO)
	{
		String addSQL="update BAS_T_CODE_TYPE set CODE_TYPE_NAME=?,DESCRIPT=? where CODE_TYPE_ID=?";
		conn=DBUtil.getConnection();
		try{
			PreparedStatement ps= conn.prepareStatement(addSQL);
			ps.setString(1, codetypeVO.getCodeTypeName());
			ps.setString(2, codetypeVO.getDescript());
			ps.setLong(3, codetypeVO.getCodeTypeId());
			ResultSet rs = ps.executeQuery();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	 /**
	  * 数组转换为字符串
	  * @param strs
	  * @return
	  */
	  
	public static String array2String(String[] strs)
	{
		if(strs==null)
		{
			return "";
		}
		String result="";
		for (String string : strs) {
			
			result+=string+",";
		}
		return result.substring(0,result.length()-1);
	}
	
	
	public Page getCodeTypePage(int pageNo,int pageSize,Object codetype)
	{		
		return this.find(codetype, pageNo, pageSize, "BAS_T_CODE_TYPE");		
	}
	
	@Override
	/**
	 * 将查询结果转换成codetype类集，并保存进List集合中
	 */
	public List ResultSet2List(ResultSet rs) {
		// TODO Auto-generated method stub
		List<CodeType> resultList=new ArrayList<CodeType>();
		try {
			while(rs!=null&&rs.next())
			{
				CodeType codetype=new CodeType();
				codetype.setCodeTypeId(rs.getLong("CODE_TYPE_ID"));
				codetype.setCodeTypeName(rs.getString("CODE_TYPE_NAME"));
				codetype.setDescript(rs.getString("DESCRIPT"));
				resultList.add(codetype);
			}
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultList;
	}
	
	@Override
	/**
	 * 添加查询条件
	 */
	public Map<Integer, Object[]> getCondition(Object o) {
		// TODO Auto-generated method stub
		Map<Integer,Object[]> codetypeMap=new HashMap<Integer,Object[]>();
		Integer count=0;
		CodeType codetype=(CodeType)o;
		if(codetype.getCodeTypeId()!=null&&codetype.getCodeTypeId()>0)
		{
			count++;
			codetypeMap.put(count, new Object[]{" and CODE_TYPE_ID LIKE ?","%"+codetype.getCodeTypeId()+"%"});
		}
		if(codetype.getCodeTypeName()!=null&& codetype.getCodeTypeName().trim().length()>0)
		{
			count++;
			codetypeMap.put(count, new Object[]{"and CODE_TYPE_NAME LIKE ?","%"+codetype.toString()+"%"});
		}
		return codetypeMap;
	}
}
